<template>
	<view class="pageStyle">
		<!-- <g-watermark/> -->
		<web-view :src="urls" @message="messageCallback"></web-view>
	</view>
</template>
<script>
	import {
		Api
	} from "@/utils/Api.js";
	import {
		mapState
	} from "vuex";

	export default {
		data() {
			return {
				describe: "通用编辑",
				url: {
					list: "/m/visual/moduleAddEditPage",
				},
				urls: "",
			};
		},
		onLoad(options) {
			let records = JSON.parse(options.record);
			console.log('options.record', options.record);
			let scanActionType = '',
				scanModuleCode = '',
				scanTargetField = '',
				scanId = '',
				treeNodeCode = '';
			if (records) {
				scanActionType = records.scanActionType;
				scanModuleCode = records.scanModuleCode;
				scanTargetField = records.scanTargetField;
				scanId = records.scanId;
				treeNodeCode = records.treeNodeCode ? records.treeNodeCode : '';
			}
			console.log('this.loginInfo', this.loginInfo);
			// records中的id有可能为模块详情或编辑页中的自定义按钮（添加模块）点击后传入的ID，所以要判断是否为数字
			if (records.id && typeof records.id === 'number' && !isNaN(records.id)) {
				//编辑
				this.urls =
					Api.baseUrl +
					this.url.list +
					"?skey=" +
					this.loginInfo.skey +
					"&moduleCode=" +
					records.code +
					"&id=" +
					records.id +
					"&isUniWebview=true";
			} else {
				if (scanModuleCode) {
					this.urls =
						Api.baseUrl +
						this.url.list +
						"?skey=" +
						this.loginInfo.skey +
						"&moduleCode=" +
						scanModuleCode +
						'&scanActionType=' + scanActionType + '&scanTargetField=' + scanTargetField + '&scanId=' + scanId +
						"&isUniWebview=true";
				} else {
					// rowIds 来自于按钮的添加模块事件
					this.urls =
						Api.baseUrl +
						this.url.list +
						"?skey=" +
						this.loginInfo.skey +
						"&moduleCode=" +
						records.code +
						"&rowIds=" +
						records.moduleId +
						"&curModuleCode=" +
						records.curModuleCode +
						"&isUniWebview=true" +
						"&treeNodeCode=" + treeNodeCode;

					// 设置页面的标题栏名称
					uni.setNavigationBarTitle({
						title: '新增'
					});
				}

				console.log('this.urls', this.urls);

			}
		},
		onBackPress(options) {
			console.log('页面返回', options);

			if (options && options.from === 'navigateBack') {
				console.log('从上一个页面返回');
				var pages = getCurrentPages();
				// var currPage = pages[pages.length - 1]; //当前页面
				var prevPage = pages[pages.length - 2]; //上一个页面
				//直接调用上一个页面的setData()方法，把数据存到上一个页面中去
				prevPage.isDoRefresh = true;
				// 这里可以编写处理返回事件的相应逻辑
			} else {
				console.log('直接按手机物理返回键返回');

				// 这里可以编写处理返回事件的相应逻辑
			}

		},
		computed: {
			...mapState(["loginInfo"]),

			// urls() {
			//   let urls = Api.baseUrl + this.url.list + this.loginInfo.skey;
			//   return urls;
			// },
		},
		methods: {
			messageCallback(e) {
				// 用于小程序端通过Webview传来的事件中刷新页面
				console.log("messageCallback", e)
				if (e.detail.data[0] && e.detail.data[0].res == 0) {
					const pages = getCurrentPages();
					const prevPage = pages[pages.length - 2]; // 上一个页面
					console.log("prevPage", prevPage);
					if (prevPage.route.indexOf('moduleList') != -1) {
						prevPage.$vm.search(); // 调用上个页面的刷新方法
					}
				}
			}
		}
	};
</script>

<style lang="scss" scoped>
</style>